<?php
namespace app\gongzhong\controller;
use think\Db;


class Index extends Base
{
    public $exception = ['login','loginCallback'];

    public function __construct(){
        parent::__construct();
        
        $page_info = $this->page_info;
        $page_info['title'] = '分类';

        $this->assign([
            'model'=>$this->model,
            'page_info'=>$page_info
        ]);
    }

    /**
     * @Author   CCH
     * @DateTime 2020-05-23T12:10:10+0800
     * @return   数据列表
     */
    public function login(){
        $callback = url('loginCallback',[],'',true);
        $state = '';
        $scope = 'snsapi_base';
        $oauth = & load_wechat('Oauth');
        $result = $oauth->getOauthRedirect($callback, $state, $scope);
        // 处理返回结果
        if($result===false){
            return false;
        }else{
            return redirect($result);
        }
    }

    public function loginCallback(){
        $oauth = & load_wechat('Oauth');
        $result = $oauth->getOauthAccessToken();
        if($result===false){
            return false;
        }else{
            $openid = $result['openid'];
            session('login_openid',$openid);
            return redirect(url('index/index'));
        }
    }

    public function index(){
        $openid = session('login_openid');
        $mobile = Db::name('user')->where('gz_openid',$openid)->value('mobile');
        if (!empty($mobile)) {
            return view('index', [
              'mobile'=>$mobile
            ]);
        }else{
            return view('bind', [
              
            ]);
        }
    }

    public function getLoginSms($mobile='',$is_check=0){
        if (strlen($mobile) != 11) {
            $this->error('手机号格式错误');    
        }
        $model = $this->model;
        if ($is_check == 1 && $model->where('mobile',$mobile)->count() > 0) {
            $this->error('手机号已被占用'); 
        }
        $code = rand(1000,9999);
        $sms_res = sendSms($mobile,'您的验证码是：'.$code,'得力云');
        // $code = 1234;
        // $sms_res['status'] = 1;
        if ($sms_res['status'] == 1) {
            session('sms_'.$mobile,$code);
            $this->success('发送成功');    
        }else{
            $this->error($sms_res['msg']);  
        }
    }

    public function bind(){
        $openid = session('login_openid');
        if (Db::name('user')->where('gz_openid',$openid)->count() > 0) {
            return redirect(url('index'));
        }
        if (request()->isPost()) {
            $model = $this->model;
            $fields = [
                'must'=>['mobile','verify'],
                'nomust'=>['is_cookie']
            ];
            $param = paramFilter(request()->param(),$fields);
            if (!empty($param['error_msg'])) {
                $this->error($param['error_msg']);
            }

            $mobile = $param['mobile'];
            if (!isMobile($mobile)) {
                $this->error('手机号格式错误');    
            }
            if (empty($param['verify']) || $param['verify'] != session('sms_'.$mobile)) {
                $this->error('验证码错误');       
            }
            $uid = Db::name('user')->where('mobile',$mobile)->value('id');
            if (empty($uid)) {
                $this->error('手机号未注册AGENT账号，请先注册后再绑定');    
            }else{
                Db::name('user')->where('id',$uid)->update(['gz_openid'=>$openid]);
                $this->success('绑定成功');    
            }
        }
    }

    public function unbind(){
        $openid = session('login_openid');
        Db::name('user')->where('gz_openid',$openid)->update(['gz_openid'=>null]);
        $this->success('解除成功',url('index'));    
    }


   
}
